<?php
session_start();

require_once('../configs/common.php'); 	 
require_once("../configs/db_connect.php");


if ($_POST['action'] == 'update_visited_time')
{
	  $user_id=$_SESSION['user_id'];
	  $exist=1;
	  $result= mysql_query("select * from users_online where user_id=$user_id");
	  
	  if (mysql_num_rows($result)==0) $exist=0;
	  
	  $curent_time=date('Y-m-d H:i:s');
	  $curent_time=strtotime($curent_time);
	  
	  switch ($exist)
	  {                 
			case 0:
			{
				mysql_query("INSERT INTO users_online (user_id,last_time) VALUES ('$user_id','$curent_time')");
				break;
			}
			case 1:
			{
				mysql_query("UPDATE users_online SET last_time=$curent_time where user_id=$user_id");
				mysql_query("UPDATE users_online SET total_time=total_time+30 where user_id=$user_id");
				break;
			}
	  
	  }
 }

     if ($_POST['action'] == 'get_visitors_online')
    {
          
          $curent_time=date('Y-m-d H:i:s');
          $curent_time=strtotime($curent_time);
                    
          $result= mysql_query("select * from online_visitors where ($curent_time-time)<15 ORDER BY name");
          
          if (mysql_num_rows($result)==0) return;         
          
          while($row=mysql_fetch_array($result))
		 {   
			 $name=$row['name'];			 
			 echo "<tr><td>$name</td></tr>";
		 }
     }
     //----------------------
      if ($_POST['action'] == 'get_visitors_online_json')
    {
          
          $curent_time=date('Y-m-d H:i:s');
          $curent_time=strtotime($curent_time);
                    
          $result= mysql_query("select count(*) as num from online_visitors where ($curent_time-time)<15");
          
          if (mysql_num_rows($result)==0) return; 
          $row=mysql_fetch_array($result) ;         
                 
		  $num=$row['num'];			 
		  $data_str = array('num' => $num);	 
          header('Content-Type: application/json');
          echo json_encode($data_str); 
		 
     }
     
     //---------------------
     
